﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using RN.ERP.Web.ModelService;
using RN.ERP.Web.BasicService;
using Newtonsoft.Json;
using RN.ERP.Web.Utilities;
using System.Web.Services;

namespace RN.ERP.Web.UserControl
{
    public partial class UCChangeVerify : BaseUserControl
    {
        [BindableAttribute(true)]
        public string SetID  //set表里的id
        {
            get { return hdnSetId.Value; }
            set { hdnSetId.Value = value; }
        }
        protected string CorpUserGuid
        {
            get
            {
                return UserInfo.CorpUserGuid;
            }
        }
        protected string CurrentArea
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }
        protected string RoleId
        {
            get
            {
                return UserInfo.RoleId;
            }
        }
        protected string Name
        {
            get
            {
                return UserInfo.Name;
            }
        }
        private BasicServiceClient basicService = new BasicServiceClient();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
                BindDwvHistory(SetID);
            }
        }

        private void InitPage()
        {
            if (base.UserInfo.RoleType != "Manager")
            {
                divOperation.Visible = false;
            }
            else
            {
                List<OperationResult> list = JsonConvert.DeserializeObject<List<OperationResult>>(basicService.CheckChange(SetID, UserInfo.RoleId, UserInfo.CurrentArea));
                if (list.Count > 1)
                {
                    if (list.Where(m => m.Result == true).Count() == 2)
                    {
                        divOperation.Visible = true;
                    }
                    else
                    {
                        divOperation.Visible = false;
                    }

                }
                else
                {
                    divHistory.Visible = false;
                    divOperation.Visible = false;
                }
            }
        }

        /// <summary>
        /// 显示操作记录
        /// </summary>
        /// <param name="SetID"></param>
        private void BindDwvHistory(string SetID)
        {
            string result = basicService.AuditLogs(SetID, UserInfo.CurrentArea);
            if (!string.IsNullOrWhiteSpace(result) && !result.Equals("null"))
            {
                List<User_Log> logs = JsonConvert.DeserializeObject<List<User_Log>>(result);
                dwvHistory.DataSource = logs;
                dwvHistory.DataBind();
            }

        }
    }
}